package org.moka.ioio.ioiopilot.input;

import org.moka.ioio.ioiopilot.IOIOThread;
import org.moka.ioio.ioiopilot.R;
import org.moka.ioio.ioiopilot.R.id;
import org.moka.ioio.ioiopilot.gui.CommandPanel;

import android.util.Log;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar.OnSeekBarChangeListener;
import ioio.lib.api.exception.ConnectionLostException;

public class GuiInputs extends AbstractInput implements OnSeekBarChangeListener, OnCheckedChangeListener  {

	public static final int BUTTON = 0;
	public static final int CHANNEL_1 = 1;
	public static final int CHANNEL_2 = 2;
	public static final int CHANNEL_3 = 3;
	public static final int CHANNEL_4 = 4;
	public static final int NUM_CHANNELS = 5;
	
	private ToggleButton button;
	private SeekBar channel_1;
	private SeekBar channel_2;
	private SeekBar channel_3;
	private SeekBar channel_4;
	

	public GuiInputs(CommandPanel commandPanel)
	{
		super(NUM_CHANNELS);
		
		button = (ToggleButton) commandPanel.findViewById(R.id.button);
		button.setOnCheckedChangeListener(this);
		if(button.isChecked())value[BUTTON] = MIN_VALUE;
		else value[BUTTON] = MAX_VALUE;
		
		channel_1 = (SeekBar) commandPanel.findViewById(R.id.channel_1);
		channel_1.setOnSeekBarChangeListener(this);
		channel_1.setProgress(INIT_ZERO_VALUE);
		value[CHANNEL_1] = INIT_ZERO_VALUE;		
		
		channel_2 = (SeekBar) commandPanel.findViewById(R.id.channel_2);
		channel_2.setOnSeekBarChangeListener(this);
		channel_2.setProgress(INIT_MID_VALUE);
		value[CHANNEL_2] = INIT_MID_VALUE;
		
		channel_3 = (SeekBar) commandPanel.findViewById(R.id.channel_3);
		channel_3.setOnSeekBarChangeListener(this);
		channel_3.setProgress(INIT_MID_VALUE);
		value[CHANNEL_3] = INIT_MID_VALUE;
		
		channel_4 = (SeekBar) commandPanel.findViewById(R.id.channel_4);
		channel_4.setOnSeekBarChangeListener(this);
		channel_4.setProgress(INIT_MID_VALUE);
		value[CHANNEL_4] = INIT_MID_VALUE;
	}
	


	@Override
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		if(seekBar.getId()==R.id.channel_1)
		{
			value[CHANNEL_1] = progress;
			//Log.v(IOIOThread.TAG, "SeekBar "+seekBar.getId()+" progress:"+progress);
		}
		else if(seekBar.getId()==R.id.channel_2) value[CHANNEL_2] = progress;
		else if(seekBar.getId()==R.id.channel_3) value[CHANNEL_3] = progress;
		else if(seekBar.getId()==R.id.channel_4) value[CHANNEL_4] = progress;
		
		
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {}
	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {}
	
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if(isChecked)value[BUTTON] = MIN_VALUE;
		else value[BUTTON] = MAX_VALUE;
	}

}
